package com.wswy.wzcx.ui.car.remind

import android.animation.ValueAnimator
import android.arch.lifecycle.Observer
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import com.wswy.wzcx.R
import com.wswy.wzcx.model.car.CarStatus
import com.wswy.wzcx.model.car.Inspection
import com.wswy.wzcx.model.home.RPaths
import com.wswy.wzcx.model.home.RouterUtils
import com.wswy.wzcx.module.base.CBaseFragment
import com.wswy.wzcx.statistics.StatTools
import com.wswy.wzcx.statistics.StatisticsId
import com.wswy.wzcx.ui.Constants
import com.wswy.wzcx.ui.activity.ModuleActivity
import kotlinx.android.synthetic.main.fragment_info_inspection.*
import kotlin.math.abs

/**
 * 年检提醒
 */
class InspectionInfoFragment : CBaseFragment() {


    private lateinit var remindViewModel: RemindViewModel

    private var nextShow:(()->Unit)?=null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_info_inspection,container,false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        remindViewModel = provideViewModelFromActivity(RemindViewModel::class.java)

        tv_car_no.text = remindViewModel.userCarInfo.carNo

        remindViewModel.userCarInfo.carExtendInfo?.inspection?.let {
            showInfo(it)
        }


        remindViewModel.getAddInsuranceLiveData().observe(this, Observer {
            it?.run {

                if (isOk()){
                    data?.inspection?.run {
                        showInfo(this)
                    }
                }
            }
        })
        //编辑
        img_edit.setOnClickListener {

            StatTools.sendClick(context, StatisticsId.addinspection_edit)

            (activity as? RemindActivity)?.run {
                showAdd()
            }
        }

        //办理年检
        btn_handle.setOnClickListener {

//            remindViewModel.userCarInfo.carExtendInfo?.inspection?.link?.run {
//
//                if (!TextUtils.isEmpty(this)){
//                    startActivity(RouterUtils.getWebIntent(it.context,this))
//                }
//
//            }
            ModuleActivity.getTargetIntent(it.context,RPaths.NJDB,Bundle().apply {
                putParcelable(Constants.EXTRA_USER_CAR_INFO,remindViewModel.userCarInfo)
            })?.let {
                startActivity(it)
            }

            StatTools.sendClick(context, StatisticsId.addinspection_handle)
        }
    }


    private fun showInfo(info: Inspection){


        if (isHidden){
            nextShow = {
                showDays(info)
            }
        }else{
            showDays(info)
        }

        when(info.status){
            CarStatus.NORMAL ->{
                //未到投保期
                val color = ContextCompat.getColor(btn_handle.context,R.color.textColorPrimary_40)
                tv_remain_days.setTextColor(ContextCompat.getColor(btn_handle.context,R.color.textColorPrimary))


                tv_remain_days_hint.text = "距离年检到期天数"


                tv_remain_days_hint.setTextColor(color)

                tv_status_desc_title.text = "目前不需要年检，可以放心驾驶"
                tv_status_desc_hint.text = info.desc
                btn_handle.isEnabled = false
                btn_handle.text = "年检已经办理完成"
            }
            CarStatus.PROMPT -> {
                //已到保险期

                val color = ContextCompat.getColor(btn_handle.context,R.color.accentColor1)
                tv_remain_days.setTextColor(color)

                tv_remain_days_hint.text = "距离年检到期天数"
                tv_remain_days_hint.setTextColor(color)

                tv_status_desc_title.text = "当前已临近年检办理期限，请及时办理"
                tv_status_desc_hint.text = info.desc

                btn_handle.isEnabled = true
                btn_handle.text = "办理年检"
            }
            CarStatus.OVERDUE -> {
                //已过投保期

                val color = ContextCompat.getColor(btn_handle.context,R.color.accentColor3)
                tv_remain_days.setTextColor(color)

                tv_remain_days_hint.text = "年检逾期天数"
                tv_remain_days_hint.setTextColor(color)
                tv_status_desc_title.text = "您的年检办理已经逾期，请及时办理"
                tv_status_desc_hint.text = "请尽快为您的年检，以免影响您的正常出行"

                btn_handle.isEnabled = true
                btn_handle.text = "办理年检"
            }
        }

    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)

        if (!hidden && nextShow != null){
            remindViewModel.userCarInfo.carExtendInfo?.insuranceInfo?.run {
                nextShow?.invoke()

                nextShow = null
            }
        }
    }

    private fun showDays(info: Inspection){

        if (isHidden){
            return
        }
        val days =  abs(info.remainDay)

        tv_remain_days?.text = 0.toString()


        ValueAnimator.ofInt(0,days).apply {
            duration = 1000
            interpolator = AccelerateDecelerateInterpolator()

            addUpdateListener {

                (it.animatedValue as? Int)?.run {
                    tv_remain_days?.text = toString()
                }

            }


        }.run {
            start()
        }

    }


}